/**
 * Created by ayu on 2017/6/9.
 */
let logger = require ('../utils/logger/Logger');
let rawDataService = require ('../models/RawDataService');
let sarftCmdmsService = require ('../models/SarftCmdmsTaskService');
let rawDataDao = require('../daos/RawDataDao');
module.exports = {
    saveRawData: function ( req, res, next )
    {
        // let data = eval ('(' + req.body + ')');
        let data = req.body;
        //若无影片信息
        if (data.data.length === 0)
        {
            logger.warn ('影厅：' + data.cinema_code + ' 上报信息为空');
            res.json ({
                "status": "error",
                "msg"   : "观看影片信息为空"
            });
            return;
        }
        rawDataService.saveRawData (data, function ( done )
        {
            if (done.isSuccess)
            {
                if (done.upload_status === 2)//为2时插入 sarft_cmdms_task 任务
                {
                    //查询此次上报的原始数据，有效观影的count
                    rawDataDao.findRawDataByHas_seen(done.session_code,function(count_done){
                        if(count_done.isSuccess){
                            if(count_done.count > 0){
                                //插入任务
                                sarftCmdmsService.saveTaskInfo (done.session_code, function ( _done )
                                {
                                    if (_done)
                                    {
                                        res.json ({
                                            "status"       : "success",
                                            "session_code" : done.session_code,
                                            "upload_status": done.upload_status
                                        });
                                    }
                                    else
                                    {
                                        res.json ({
                                            "status"       : "fail",
                                            "session_code" : done.session_code,
                                            "upload_status": done.upload_status,
                                            "msg"          : "信息上报成功，任务插入或关联失败"
                                        });
                                    }
                                });
                            }
                        }else{
                            logger.error('场次：'+done.session_code+' 查询条数出错');
                            res.json ({
                                "status"       : "success",
                                "session_code" : done.session_code,
                                "upload_status": done.upload_status
                            });
                        }
                    });
                    
                }
                else
                {
                    res.json ({
                        "status"       : "success",
                        "session_code" : done.session_code,
                        "upload_status": done.upload_status
                    });
                }
            }
            else
            {
                if (!done.msg)
                {
                    res.json ({
                        "status"      : "fail",
                        "session_code": done.session_code,
                        "msg"         : "改变任务状态失败"
                    });
                }
                else
                {
                    res.json ({
                        "status"      : "error",
                        "session_code": done.session_code,
                        "msg"         : done.msg
                    });
                }
            }
        });
    }
};